package com.jqq.list_;

/**
 * @author 姬倩倩
 * @version 1.0
 */
public class LinkedList01 {
    public static void main(String[] args) {
        //模拟一个简单的双向链表
        Node jack = new Node("jack");
        Node tom = new Node("tom");
        Node jqq = new Node("jqq");

        //连接三个节点，形成双向链表
        //jack <-> tom <->jqq
        jack.next = tom;
        tom.next = jqq;
        jqq.pre = tom;
        tom.pre = jack;

        Node first = jack;//让jack为头节点
        Node last = jqq;//让jqq为尾节点
        System.out.println("==从头到尾遍历==");
        while (first != null) {
            System.out.println(first);
            first = first.next;
        }
        System.out.println("==从尾到头遍历==");
        while(last != null){
            System.out.println(last);
            last = last.pre;
        }
        //演示链表的添加数据的操作
        //在jack-tom之间加入张飞
        Node zf = new Node("张飞");
        zf.next = tom;
        zf.pre = jack;
        tom.pre = zf;
        jack.next = zf;
        first = jack;//让jack为头节点
        System.out.println("==从头到尾遍历==");
        while (first != null) {
            System.out.println(first);
            first = first.next;
        }
    }
}
//定义一个Node类，Node对象表示双向链表的一个节点
class Node{
    public Object item;//真正存放数据的地方
    public Node next;//指向下一个节点
    public Node pre;//指向前一个结点

    public Node(Object item) {
        this.item = item;
    }

    @Override
    public String toString() {
        return "Node name=" + item;
    }
}